//
//  array_min.hpp
//  suanfa
//
//  Created by 李贺 on 2021/11/24.
//

#ifndef array_min_hpp
#define array_min_hpp

#include <stdio.h>
#include <vector>
#include <iostream>

using namespace std;

class Solution_array_min {
    
public:
    
     int find_min_sub(vector<int> &arr, int target) {
        int pre_len = 0;
        int temp_sum = 0;
         
        for (int index = 0; index < arr.size(); index++) {
            
            temp_sum = arr[index];
            
            if (temp_sum < target) {
                int current_len = 1;
                for (int j = index + 1; j < arr.size(); j++) {
                    if (temp_sum + arr[j] < target) {
                        temp_sum = temp_sum + arr[j];
                        current_len += 1;
                    }
                }
                if (current_len != 1) {
                    if (pre_len == 0) {
                        pre_len = current_len;
                    } else if (pre_len > current_len) {
                        pre_len = current_len;
                    }
                }
            }
        }
        
        return pre_len;
    }
    
    int find_min_sub1(vector<int> &arr, int target) {
        
        if (arr.size() == 0) {
            return 0;
        }
        
        int result = INT32_MAX;
        int sum = 0; // 滑动窗口数值之和
        int i = 0; // 滑动窗口起始位置
        int subLength = 0;
        
        for (int j = 0; j < arr.size(); j++) {
            sum += arr[j];
            while (sum >= target) {
                // 计算之间的长度
                subLength = j - i + 1;
                result = result < subLength ? result : subLength;
                sum -= arr[i++]; // 这里体现出滑动窗口的精髓之处，不断变更i（子序列的起始位置）
            }
        }
        
        return 0;
    }
    
    void test() {
        int target = 7;
        vector<int> nums;
        nums.push_back(2);
        nums.push_back(3);
        nums.push_back(1);
        nums.push_back(2);
        nums.push_back(4);
        nums.push_back(3);
        
        int result = this->find_min_sub(nums, target);
        cout << result << "\n";
    }
    
};

#endif /* array_min_hpp */
